package com.example.zzm.yuejian03.Adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.zzm.yuejian03.Bean.ClassBean;
import com.example.zzm.yuejian03.R;
import com.example.zzm.yuejian03.Server;
import com.squareup.picasso.Picasso;

import java.util.List;

public class ClassAdapter extends RecyclerView.Adapter<ClassAdapter.ViewHolder> {

    private List<ClassBean> list;//向rv中填充的数据
    private Context context;//上下文
    private LayoutInflater layoutInflater;//动态布局

    //自定义 构造方法

    public ClassAdapter(Context context,List<ClassBean> list) {
        this.context = context;
        this.list = list;
        layoutInflater = LayoutInflater.from(context);
}

    //自定义 ViewHolder子类，容纳item视图
    public class ViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView;
        TextView textView,textView2,textView3;

        public ViewHolder(View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.imageView);
            textView = itemView.findViewById(R.id.textView);
            textView3 = itemView.findViewById(R.id.textView3);
            textView2 = itemView.findViewById(R.id.textView2);
        }
    }

    //自定义 设置数据list
    public void setList(List<ClassBean> list) {
        this.list = list;
        notifyDataSetChanged();//通知RV刷新数据
    }

    @NonNull
    @Override  //为每一个item生成一个view
    public ClassAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.course,parent,false);
        ClassAdapter.ViewHolder holder = new ClassAdapter.ViewHolder(view);
        return holder;
    }

    @Override  //为每个item填充数据，设置点击事件
    public void onBindViewHolder(ClassAdapter.ViewHolder holder, final int position) {
        final ClassBean bean = list.get(position);
        if (bean == null)
            return;
        ViewHolder viewHolder = (ViewHolder) holder;
        Picasso.with(context)//新版的Picasso方法改为get()
                .load(Server.IMAGEURL + bean.getPic())
                .placeholder(R.mipmap.ic_launcher)
                .into(viewHolder.imageView);
        viewHolder.textView.setText(bean.getClass_name());
        viewHolder.textView2.setText(bean.getIntro());
        viewHolder.textView3.setText(Integer.toString(bean.getPrice()));
//
//        //item条目点击事件
//        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                //取出当前item的id
//                int cl_id = bean.getid();
//                Toast.makeText(context, "" + cl_id, Toast.LENGTH_SHORT).show();
//
//                Intent intent=new Intent(context, ClubActivity.class);
//                intent.putExtra("club_id",bean.getid());
//                context.startActivity(intent);
//
//            }
//        });
    }

    @Override  //确定item个数
    public int getItemCount() {
        if(list!=null)
            return list.size();
        else
            return 0;
    }
}
